package com.iweb.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@WebFilter("/user/*") // 注意 Filter 的处理所有请求是 /* 而servlet 处理所有请求是 /
public class UserFilter implements Filter {

    /**
     * 初始化，filterConfig获取参数
     *
     * @param filterConfig
     * @throws ServletException
     */
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("UserFilter is init");
    }

    /**
     * 处理拦截方法：每次请求都会被拦截
     *
     * @param request
     * @param response
     * @param chain
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("UserFilter is doFilter");

        // 放行
        chain.doFilter(request, response);
    }

    /**
     * 销毁的方法
     */
    @Override
    public void destroy() {
        System.out.println("UserFilter is destory");
    }
}
